<?php
/**
 * 应用管理
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @author cmpan
 * @copyright   Copyright (c) 2009-2011 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
UE_Loader::loadModel('AppModel', 'system');
class AppController extends UE_Controller_Admin  {
	/**
	 * 应用模型
	 *
	 * @var AppModel
	 */
	private $_app = null;
	
	public function init() {		
		$this->_app = new AppModel();
	}
	
	public function listAction() {
		$appsInfo = array();  // application 按分类（核心|可选|其他）显示
		$installedApps = $this->_app->getApps();
		
		$d = dir(APPS_DIR);
		while (false !== ($entry = $d->read())) {
		    if ($entry[0] != '.' && is_dir(APPS_DIR.'/'.$entry)) {		    	
		    	$appInfo = $this->_app->getInfo($entry);
		    	//print_r($appInfo);
		    	empty($appInfo['app_package']) && $appInfo['app_package'] = 'other';
		    	// 取得未安装应用信息
		    	if (empty($installedApps[$entry])) {			    		
			    	$appInfo['app_id'] = $entry;
			    	$appInfo['app_activated'] = false;
			    	$appInfo['app_installed'] = false;
			    	$appsInfo[$appInfo['app_package']][$entry] = $appInfo;
		    	} else {
		    		// 取得已安装的应用的信息
		    		$appsInfo[$appInfo['app_package']][$entry] = $installedApps[$entry];
		    	}
		    }
		}
		
		$d->close();
		ksort($appsInfo);
		
		// 操作说明
		$tips = array();
		$tips[] = '启用：开始使用一个应用，系统自动设置该应用的用户访问权限';
		$tips[] = '停用：停止使用一个应用，保留该应用的用户访问权限的设置，重新启用时不必重新设置访问权限。';
		$tips[] = '卸载：停止使用一个应用，并且去掉该应用的用户访问权限设置。（为安全起见，程序不自动删除该应用目录及文件）';
		
		//print_r($appsInfo);
		include $this->tpl();
	}
	
	public function installAction() {
		if (empty($this->id)) {
			print 'Error Param';
			return ;
		}
		
		if($this->_app->install($this->id)){
			$this->setOK('成功安装应用： '.$this->id);
			UE_Cache::factory()->delete('apps');
		} else {
			$this->setErr($this->_app->getErrs());
		}
		
		$this->setAct('list');
		$this->listAction();
	}
	
	public function uninstallAction() {
		if (empty($this->id)) {
			print 'Error Param';
			return ;
		}
		
		if($this->_app->uninstall($this->id)){
			$this->setOK('成功卸载应用： '.$this->id);
			UE_Cache::factory()->delete('apps');
		} else {
			$this->setErr($this->_app->getErrs());
		}
		
		$this->setAct('list');
		$this->listAction();
	}
	
	/**
	 * 禁用应用
	 *
	 */
	public function deactivateAction() {
		if (empty($this->id)) {
			print 'Error Param';
			return ;
		}if($this->_app->deactivate($this->id)){
			$this->setOK('成功禁用应用： '.$this->id);
			UE_Cache::factory()->delete('apps');
		} else {
			$this->setErr($this->_app->getErrs());
		}
		
		$this->setAct('list');
		$this->listAction();
	}
	
	/**
	 * 启用应用
	 *
	 */
	public function activateAction() {
		if (empty($this->id)) {
			print 'Error Param';
			return ;
		}if($this->_app->activate($this->id)){
			$this->setOK('成功启用应用： '.$this->id);
			UE_Cache::factory()->delete('apps');
		} else {
			$this->setErr($this->_app->getErrs());
		}
		
		$this->setAct('list');
		$this->listAction();
	}
	
	/**
	 * 删除应用
	 *
	 */
	public function delAction() {
		if (empty($this->id)) {
			print 'Error Param';
			return ;
		}if($this->_app->setId($this->id)->delete()){
			$this->setOK('成功删除应用： '.$this->id);
			UE_Cache::factory()->delete('apps');
		} else {
			$this->setErr($this->_app->getErrs());
		}
		
		$this->setAct('list');
		$this->listAction();
	}
	
	/**
	 * 权限设置
	 *
	 */
	public function aclsettingAction() {
		UE_Loader::loadModel('RoleModel', 'user');
		UE_Loader::loadModel('AppModel', 'system');
		$appModel = new AppModel();
		$role = new RoleModel();
				
		if (IS_POST) {			
			if(!empty($_POST['acl_item'])) {
				foreach ($_POST['acl_item'] as $aclApp=>$appCtls) {
					// 把该应用已设置的权限删除后在添加
					$sql = "DELETE FROM ue_acl WHERE acl_app='$aclApp'";
					UE_DB::factory()->query($sql);
					foreach ($appCtls as $aclCtl=>$appActs) {
						foreach ($appActs as $aclAct=>$appRols) {
						    foreach ($appRols as $aclRoleId=>$_tmp) {
						    	$appModel->addAcl($aclApp, $aclCtl, $aclAct, $aclRoleId, 1);						    	
						    }
						}
					}					
				}
				
			    UE_Cache::factory()->delete('apps');
			}
		}
		
		$roles = $apps = $aclsList = $actsList = array();
		$roles = $role->getRoles('', false);
		$apps = $appModel->getApps();
		
		if ($this->id) {			
			if (empty($apps[$this->id])) {
				$this->setErr('该应用不存在！');
			} else {			
				$aclsList[$this->id] = $appModel->getAcls($this->id);
				$actsList[$this->id] = $appModel->getActs($this->id);
			}
		} else {
			$aclsList = $appModel->getAcls();
			$actsList = $appModel->getActs();
		}
		
		//print_r($apps);
		include $this->tpl();
	}
	
	public function claerCacheAction() {
		UE_Cache::factory()->clear();
		$this->setOK('成功删除缓存');
		$this->showMessage();
	}
}
